﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoCaiWeb.WebSite.Code;
using BoCaiWeb.Model;
using BoCaiWeb.Business;
using System.Collections;
using Ajeelee.Core.Utils;

namespace BoCaiWeb.WebSite.WebAdmin
{
    public partial class SysModuleEdit : AuthPageBase
    {
        protected bool isUpdate;
        protected string id = "";
        protected string pid = "0";

        Hashtable table = new Hashtable();
        protected SysModule sysModule = new SysModule();
        ISysModuleBusiness sysModuleBll = new SysModuleBusinessFactory().CreateSysModuleBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                id = QueryParam("id");
                pid = QueryParam("pid", "0");
                if (!string.IsNullOrEmpty(id))
                {
                    isUpdate = true;
                    sysModule = sysModuleBll.GetById(id);
                    if (sysModule == null)
                    {
                        sysModule = new SysModule();
                        MessageBoxGo("不存在该信息", "SysModules.aspx");
                    }
                    else
                    {
                        id = sysModule.Id;
                        pid = sysModule.PId;
                    }
                }
                else
                {
                    if (IsGet)
                    {
                        table.Clear();
                        table.Add(SysModuleHash.TopNum, 1);
                        table.Add(SysModuleHash.PId, pid);
                        table.Add(SysModuleHash.OrderField, "Id");
                        IList<SysModule> sysModuleList = sysModuleBll.GetAllSysModule(table);
                        if (sysModuleList.Count > 0)
                        {
                            id = sysModuleList.FirstOrDefault().Id;
                        }
                        if (id.Length == 2)
                        {
                            int _id = id.ToInt() + 1;
                            id = (_id < 10 ? "0" : "") + (_id).ToString();
                        }
                        else if (id.Length == 4)
                        {
                            int _id = id.ToInt() + 1;
                            id = (_id < 1000 ? "0" : "") + (_id).ToString();
                        }
                        else
                        {
                            id = (pid.IsNotNullAndEmpty() && pid != "0" ? pid : "") + "01";
                        }
                    }
                }

                if (IsPost)
                {
                    // 更新
                    if (!string.IsNullOrEmpty(id))
                    {
                        initModel();
                        sysModuleBll.Update(sysModule);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("SysModules.aspx", true);

                    }
                    else // 新建
                    {
                        initModel();
                        // 先检查一下
                        table.Clear();
                        table.Add(SysModuleHash.TopNum, 1);
                        table.Add(SysModuleHash.PId, pid);
                        table.Add(SysModuleHash.OrderField, "Sort");
                        IList<SysModule> sysModuleList = sysModuleBll.GetAllSysModule(table);
                        if (sysModuleList.Count > 0)
                        {
                            sysModule.Sort = sysModuleList.FirstOrDefault().Sort + 1;
                        }

                        sysModuleBll.Insert(sysModule);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("SysModules.aspx", true);
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }
        public void initModel()
        {
            // 就初始首页xml
            if (string.IsNullOrEmpty(id))
            {
                sysModule = new SysModule();
                sysModule.Id = FormParam("mid");
                sysModule.PId = FormParam("pid");
            }
            else
            {
                sysModule.Id = id;
                sysModule.PId = pid;
            }
            if (sysModule.Author.IsNullOrEmpty())
            {
                sysModule.Author = User.Identity.Name;
                sysModule.InIP = IPAddress;
            }
            sysModule.Name = FormParam("mname");
            sysModule.Href = FormParam("href");
        }
    }
}